package cn.iocoder.yudao.module.blog.service.post;

import cn.iocoder.yudao.module.blog.dal.dataobject.space.BlogSpaceDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.post.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.post.BlogPostDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.post.BlogPostConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.post.BlogPostMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 帖子 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogPostServiceImpl implements BlogPostService {

    @Resource
    private BlogPostMapper blogPostMapper;

    @Override
    public Long createPost(BlogPostCreateReqVO createReqVO) {
        // 插入
        BlogPostDO post = BlogPostConvert.INSTANCE.convert(createReqVO);
        blogPostMapper.insert(post);
        // 返回
        return post.getId();
    }

    @Override
    public void updatePost(BlogPostUpdateReqVO updateReqVO) {
        // 校验存在
        validatePostExists(updateReqVO.getId());
        // 更新
        BlogPostDO updateObj = BlogPostConvert.INSTANCE.convert(updateReqVO);
        blogPostMapper.updateById(updateObj);
    }

    @Override
    public void deletePost(Long id) {
        // 校验存在
        validatePostExists(id);
        // 删除
        blogPostMapper.deleteById(id);
    }

    private void validatePostExists(Long id) {
        if (blogPostMapper.selectById(id) == null) {
            throw exception(POST_NOT_EXISTS);
        }
    }

    @Override
    public BlogPostDO getPost(Long id) {
        return blogPostMapper.selectById(id);
    }

    @Override
    public List<BlogPostDO> getPostList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return blogPostMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogPostDO> getPostPage(BlogPostPageReqVO pageReqVO) {
        return blogPostMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogPostDO> getPostList(BlogPostExportReqVO exportReqVO) {
        return blogPostMapper.selectList(exportReqVO);
    }

    @Override
    public void updatePostLikeNum(Long postId, Integer value){
        validatePostExists(postId);
        BlogPostDO blogPostDO = blogPostMapper.selectById(postId);
        blogPostDO.setLikeNum(blogPostDO.getLikeNum() + value);
        blogPostMapper.updateById(blogPostDO);
    }

    @Override
    public void updatePostCommentNum(Long postId, Integer value){
        validatePostExists(postId);
        BlogPostDO blogPostDO = blogPostMapper.selectById(postId);
        blogPostDO.setCommentNum(blogPostDO.getCommentNum() + value);
        blogPostMapper.updateById(blogPostDO);
    }

}
